<?php

namespace app\api\controller;

use app\api\model\Brand as BrandModel;
use app\api\model\Config as ConfigModel;
use app\api\model\SystemConfig;
use app\api\model\UserCar as UserCarModel;
use app\common\controller\Api;
use think\Db;
use wxpay\Wxtuisong;
use app\api\model\UserShop as UsershopModel;
use app\api\model\Goods as GoodsModel;
use app\api\model\User as UserModel;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        if (!empty($ip) && preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/',$ip))
        {
            countvisit($ip);
        }
    }
    /**
     * 获取平台分类
     */
    public function getPlatform()
    {
        $response = Db::name('category')->where('type','platform')->where('status','normal')->order('weigh desc')->select();
        $this->success('获取成功',$response,200);
    }
    /**
     * 获取商品分类
     */
    public function category()
    {
        $id = $this->request->param('id',0);
        $where['pid'] = ['eq',$id];
        $where['status'] = ['eq',0];
        if ($id)
        {
            $categoryList = Db::name('goods_category')->where($where)->order('weigh desc')->select();
            foreach ($categoryList as $k=>$v)
            {
                $where['pid'] = ['eq',$v['id']];
                $categoryList[$k]['catelist'] = Db::name('goods_category')->where($where)->order('weigh desc')->select();
            }
        }else{
            $categoryList = Db::name('goods_category')->where($where)->order('weigh desc')->select();
        }

        $this->success('获取成功',$categoryList,200);
    }
    /**
     * 四级分类
     */
    public function getCategoryId()
    {
        $id = $this->request->param('id',0);
        if (!$id) $this->success('请选择查询的分类','',400);
        $categoryList = Db::name('goods_category')->where('pid',$id)->order('weigh desc')->select();
        $this->success('获取成功',$categoryList,200);
    }
    /**
     *新品推荐
     */
    public function getNewGoods()
    {
        $uid  = $this->auth->id;
        $limit = $this->request->param('limit',20);
        $page  = $this->request->param('page',1);
        $name  = $this->request->param('name','');
        $category = $this->request->param('category','');
        $GoodsModel = new GoodsModel();
        $response = $GoodsModel->getIsnew($uid,$page,$limit,$name,$category);

        if ($response)
        {
            $this->success('获取成功',$response,200);
        }else{
            $this->success('暂无数据','',400);
        }

    }
    /**
     * 获取热销产品
     */
    public function getGoodsHot()
    {
        $uid = $this->auth->id;
        $limit = $this->request->param('limit',20);
        $page  = $this->request->param('page',1);

        $GoodsModel = new GoodsModel();
        $UserModel = new UserModel();
        $where = [];
        $where['is_new'] = ['eq',1];
        $where['status'] = ['eq',0];

        $getUser = $UserModel->getUserInfo($uid);
        $goods = $GoodsModel->Getgoodshot($getUser);

        if ($goods)
        {
            $ConfigModel = new ConfigModel();
            $Agent = $ConfigModel->getAgent();
            $OneAgent = $ConfigModel->getOneAgent();
            foreach ($goods as $key => $val)
            {
                if ($getUser['is_vip'] == 0)
                {
                    $goods[$key]['price'] = $val['ordinaryprice'];
                }elseif ($getUser['is_vip'] == 1)
                {
                    $goods[$key]['price'] = $val['agentprice'] == 0 ? $val['haveprice'] : $val['agentprice'];
                }elseif ($getUser['is_vip'] == 2)
                {
                    if ($getUser['data']['is_price'] == 1)
                    {
                        $goods[$key]['price'] = $val['agentprice'] == 0 ? bcmul($val['haveprice'], $Agent, 2) : $val['agentprice'];
                    }else{
                        $goods[$key]['price'] = $val['agentprice'] == 0 ? $val['haveprice'] : $val['agentprice'];
                    }
                }elseif ($getUser['is_vip'] == 3){
                    if ($getUser['data']['is_price'])
                    {
                        $goods[$key]['price'] = $val['agentprice'] == 0 ? bcmul($val['haveprice'], $OneAgent, 2) : $val['agentprice'];
                    }else{
                        $goods[$key]['price'] = $val['agentprice'] == 0 ? $val['haveprice'] : $val['agentprice'];
                    }
                }
            }

            $this->success('获取成功',['list' => $goods, 'count' => count($goods)],200);
        }else{
            $this->success('没有新品','',200);
        }
    }
    /**
     * 协议
     */
    public function agreement()
    {
        $type = $this->request->param('type');
        $ConfigModel = new ConfigModel();
        $agreData = $ConfigModel->getAgreement($type);

        $this->success('获取成功',$agreData,200);
    }
    /**
     * 安装说明
     */
    public function getInstr()
    {
        $ConfigModel = new ConfigModel();
        $name = 'instructions';
        $group = 'basic';
        $agreData = $ConfigModel->getInstallation($name,$group);
        $this->success('获取成功',$agreData['value'],200);
    }
    /**
     * 积分规则
     *
     **/
    public function getIntegr()
    {
        $ConfigModel = new ConfigModel();
        $integralrules = $ConfigModel->getIntegralrules();
        $this->success('获取成功',$integralrules,200);
    }
    /**
     * 获取品牌
     */
    public function getbrand()
    {
        $BrandModel = new BrandModel();
        $data = $BrandModel->Getbrandlist();
        $this->success('获取成功',$data,200);
    }
    /**
     * 系统通知
     */
    public function systemConfig()
    {
        $id = $this->request->post('id');
        if (!empty($id))
        {
            $geSystem = SystemConfig::getSystemConfigId($id);

            $this->success('获取成功', $geSystem, 200);
        }

        $data = SystemConfig::getSystemConfig();
        $data = empty($data) ? [] : $data;
        $this->success('获取成功', $data, 200);
    }
    /**
     * 签到积分
     */
    public function signin()
    {
      $data = ConfigModel::getSing();
      $this->success('获取成功', $data, 200);
    }
    /**
     * 分享积分
     */
    public function sharesign()
    {
        $confingmodel = new ConfigModel();
        $data = $confingmodel->Shareintegral();
        $this->success('获取成功', $data, 200);
    }
    /**
     * 条约
     */
    public function treaty()
    {
        $data = ConfigModel::rules('treaty');
      
        if ($data)
        {
            $this->success('获取成功', $data, 200);
        }else{
            $this->success('暂无数据', '', 400);
        }
    }
    //取消订单消息推送
    public function qudingtui(){
        $order_id = $this->request->param("order_id");
        $wxtui = new Wxtuisong();
        $UserModel = new UserModel();
        $uid = $this->auth->id;
        $getUser = $UserModel->getUserInfo($uid);
        $touser = $getUser["data"]["openid"];
        
        $data = $wxtui->sendqudingMessage($order_id,$touser);
        $this->success('获取成功', $data, 200);
    }
    //资质审核消息订阅
    // public function zizhitui(){
    //     $wxtui = new Wxtuisong();
    //     $UserModel = new UserModel();
    //     $uid = $this->auth->id;
    //     $getUser = $UserModel->getUserInfo($uid);
    //     $touser = $getUser["data"]["openid"];
        
    //     $data = $wxtui->sendshenheMessage($touser,$result,$notes);
    //     $this->success('获取成功', $data, 200);
    // }
}
